package com.example.service.impl;

import com.example.dao.CarDao;
import com.example.entity.result.Result;
import com.example.entity.vo.Car;
import com.example.entity.vo.Order;
import com.example.exception.OperatorException;
import com.example.service.CarService;
import com.example.utils.DataUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private CarDao carDao;

    //查询
    @Override
    public Result query(String key, String value) {
        DataUtils.paramCheckEmpty(key, value);
        switch (key) {
            case "all":
                return DataUtils.getResult(Result.SUCCESS, "carAll",
                        carDao.selectAll());
            case "byId":
                return DataUtils.getResult(Result.SUCCESS, "car",
                        carDao.selectById(DataUtils.paramToInteger(value)));
            case "byUser":
                return DataUtils.getResult(Result.SUCCESS, "car",
                        carDao.selectByUser(DataUtils.paramToInteger(value)));
            case "byType":
                return DataUtils.getResult(Result.SUCCESS, "car",
                        carDao.selectByType(value));
            case "byDate":
                HashMap<String, String> map = DataUtils.jsonToMap(value);
                DataUtils.paramCheckEmpty(key, value);
                return queryByAllowDate(map);
            case "byList":
                return queryByOrderList(value);
            case "bySearch":
                return queryBySearch(value);
            default:
                return DataUtils.getResult(Result.PARAMETER_INVALID);
        }
    }

    private Result queryBySearch(String value) {
        List<Car> data = new ArrayList<>();
        String[] split = value.split(",");
        for (String s : split) {
            List<Car> cars = carDao.selectByTypeName(s);
            data.addAll(cars);
        }
        if (data.isEmpty()) {
            return DataUtils.getResult(Result.DATA_NULL, "data null");
        }
        return DataUtils.getResult(Result.SUCCESS, "", data);
    }

    private Result queryByOrderList(String value) {
        ObjectMapper objectMapper = DataUtils.getObjectMapper();
        List<Order> orders;
        List<Car> cars = new ArrayList<>();
        try {
            orders = objectMapper.readValue(value, new TypeReference<List<Order>>() {
            });
        } catch (JsonProcessingException e) {
            throw new OperatorException(e.getMessage());
        }
        for (Order order : orders) {
            cars.add(carDao.selectById(order.getCid()));
        }
        if (cars.isEmpty()) {
            return DataUtils.getResult(Result.DATA_NULL, "data null");
        }
        return DataUtils.getResult(Result.SUCCESS, "car", cars);
    }

    //根据起始时间查询
    private Result queryByAllowDate(HashMap<String, String> map) {
        if (map.get("start") == null || map.get("start") == null) {
            return DataUtils.getResult(Result.PARAMETER_MISSING, "缺少参数");
        }
        String start = map.get("start");
        String end = map.get("end");
        return DataUtils.getResult(Result.SUCCESS, "car",
                carDao.selectByAllowDate(start, end));
    }

    //添加
    @Override
    public Result insertCar(Car car) {
        if (DataUtils.dataCompare(car, new Car(), Car.class) == 1.0) {
            return DataUtils.getResult(Result.PARAMETER_EMPTY);
        }
        if (carDao.selectById(car.getCid()) != null) {
            return DataUtils.getResult(Result.ERROR, "该车辆已存在");
        }
        try {
            int add = carDao.add(car);
            return (add > 0) ? DataUtils.getResult(Result.SUCCESS, "添加成功") :
                    DataUtils.getResult(Result.ERROR, "添加失败");

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return DataUtils.getResult(Result.ERROR, "添加失败");
    }

    //修改
    @Override
    public Result updateCar(Car car) {
        if (carDao.selectById(car.getCid()) == null) {
            return DataUtils.getResult(Result.ERROR, "该车辆不存在");
        }
        if (DataUtils.dataCompare(car, carDao.selectById(car.getCid()), Car.class) == 1.0) {
            return DataUtils.getResult(Result.SUCCESS, "无更新");
        }
        int update = carDao.update(car);
        return (update > 0) ? DataUtils.getResult(Result.SUCCESS, "修改成功") :
                DataUtils.getResult(Result.ERROR, "修改失败");
    }

    //删除
    @Override
    public Result deleteCar(int cid) {
        if (carDao.selectById(cid) == null) {
            return DataUtils.getResult(Result.ERROR, "找不到该车辆");
        }
        int delete = carDao.delete(cid);
        return (delete > 0) ? DataUtils.getResult(Result.SUCCESS, "删除成功") :
                DataUtils.getResult(Result.ERROR, "删除失败");
    }

    @Override
    public Result updateState(int cid, int state) {
        Car car = carDao.selectById(cid);
        if (car == null) {
            return DataUtils.getResult(Result.ERROR, "找不到该车辆");
        }
        car.setCState(state);
        int update = carDao.updateState(car);
        return (update > 0) ? DataUtils.getResult(Result.SUCCESS, "修改成功") :
                DataUtils.getResult(Result.ERROR, "修改失败");
    }

}
